﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Library.Logging;
using SaleManagement.Domains;
using SaleManagement.Services;
using System.Xml;
using System.Collections.Generic;

namespace SaleManagement.Web.App_Code
{
  public class BasePage : Page
  {
    private XmlDataSource m_xmlData;

    #region Events
    protected override void OnError(EventArgs e)
    {
      Exception ex = Server.GetLastError();
      Logger.LogError("BasePage::OnError.", ex, DefaultLoggingCategory, null);
    }

    protected override void OnLoad(EventArgs e)
    {
      if (IsCallback)
        System.Threading.Thread.Sleep(500);

      base.OnLoad(e);
    }

    protected override void OnInit(EventArgs e)
    {
      string curUrl = Request.Url.AbsoluteUri.ToLower();
      base.OnInit(e);
      if (!Page.IsCallback)
      {
        if (CurrentUser == null && !curUrl.Contains("login.aspx"))
        {
          Response.Redirect("~/Pages/Login.aspx");
        }
      }
    }

    protected override void InitializeCulture()
    {
      base.InitializeCulture();
      LoadLanguage();
    }
    #endregion

    #region Properties

    private void LoadLanguage()
    {
      Page.Culture = "vi-VN";
      Page.UICulture = "vi-VN";
    }

    internal User CurrentUser
    {
      get { return Session["CurrentUser"] as User; }
      set { Session["CurrentUser"] = value; }
    }

    internal TList<UserFunction> CurrentUserFunctions
    {
      get { return Session["CurrentUserFunctions"] as TList<UserFunction>; }
      set { Session["CurrentUserFunctions"] = value; }
    }

    internal string ErrorMessage
    {
      get { return Session["ErrorMessage"] as string; }
      set { Session["ErrorMessage"] = value; }
    }

    public const string DefaultLoggingCategory = "General";

    public string SelectedMenuItem
    {
      set
      {
        Session["SelectedMenuItem"] = value;
      }
      get
      {
        if (Session["SelectedMenuItem"] == null)
        {
          return null;
        }

        return (string)Session["SelectedMenuItem"];
      }
    }

    public string SelectedMenuItemText
    {
      set
      {
        Session["SelectedMenuItemText"] = value;
      }
      get
      {
        if (Session["SelectedMenuItemText"] == null)
        {
          return null;
        }

        return (string)Session["SelectedMenuItemText"];
      }
    }

    public string SelectedMenuItemTitle
    {
      set
      {
        Session["SelectedMenuItemTitle"] = value;
      }
      get
      {
        if (Session["SelectedMenuItemTitle"] == null)
        {
          return null;
        }
        return (string)Session["SelectedMenuItemTitle"];
      }
    }

    public int SelectedMenuItemIndex
    {
      set
      {
        Session["SelectedMenuItemIndex"] = value;
      }
      get
      {
        if (Session["SelectedMenuItemIndex"] == null)
        {
          return 0;
        }
        return (int)Session["SelectedMenuItemIndex"];
      }
    }

    public bool CanModify
    {
      get { return HasPermission(14); }
    }

    internal Dictionary<string, string> CurrentSearchSession
    {
      get
      {
        Dictionary<string, string> temp = null;
        if (Session["CurrentSearchSession"] == null)
        {
          Session["CurrentSearchSession"] = new Dictionary<string, string>();
        }
        temp = (Dictionary<string, string>)Session["CurrentSearchSession"];
        return temp;
      }
      set
      {
        CurrentSearchSession = value;
      }
    }

    #endregion

    #region Methods

    public string GetSessionSearch(string key)
    {
      if (CurrentSearchSession == null) return null;
      string result = null;
      if (CurrentSearchSession.ContainsKey(key) && CurrentSearchSession.TryGetValue(key, out result))
      {
        return result;
      }
      return null;
    }

    public void ConfigSessionSearch(string key, string value)
    {
      try
      {
        if (CurrentSearchSession.ContainsKey(key))
        {
          CurrentSearchSession.Remove(key);
        }

        CurrentSearchSession.Add(key, value);
      }
      catch
      {
      }
    }

    public void WriteLog(string description, Exception ex)
    {
      Logger.LogError(description, ex, DefaultLoggingCategory, null);
    }

    public bool HasPermission(int functionId)
    {
      if (CurrentUserFunctions == null)
      {
        TList<UserFunction> listUserFunction = new UserFunctionService().GetByUsername(CurrentUser.Username);
        CurrentUserFunctions = listUserFunction;
      }

      UserFunction permission = CurrentUserFunctions.Find(delegate(UserFunction p)
      {
        return p.FunctionId == functionId;
      });

      if (permission != null)
      {
        return true;
      }

      return false;
    }

    public XmlDataSource GetStatusXmlData(string xmlPath)
    {
      return GetXmlDataSource("~/App_Data/Status.vi-VN.xml", "~/App_Data/Status.xml", xmlPath);
    }

    public string GetItemDescription(object status, string path)
    {
      if (m_xmlData == null)
        m_xmlData = GetStatusXmlData(path);

      XmlNode node = m_xmlData.GetXmlDocument().SelectSingleNode(path + "[@Id='" + status.ToString() + "']");
      if (node != null)
        return node.Attributes["Value"].Value;
      else
        return "";
    }

    public XmlDataSource GetXmlDataSource(string vnDataFile, string usDataFile, string xmlPath)
    {
      XmlDataSource dsData = new XmlDataSource();
      dsData.DataFile = vnDataFile;
      dsData.XPath = xmlPath;
      return dsData;
    }
    #endregion

    #region Need to Override
    internal virtual int RequiredPermission
    {
      get { return -1; }
    }

    public XmlDataSource XmlData
    {
      get { return m_xmlData; }
      set { m_xmlData = value; }
    }
    #endregion


  }
}
